home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / accounts / adduser.000 / adduser / adduser.shadow.1.5 / xfdes.c < prev    next >
C/C++ Source or Header  |  1996-05-10  |  11KB  |  426 lines

  1. /****************************************************************************\
  2. **      XFDES.C - FAST DES ENCRYPTION ALOGRITHM FUNCTIONS FOR TURBO-C       **
  3. ******************************************************************************
  4. **             VAX/FDES Routines Modified By Doctor Dissector               **
  5. ******************************************************************************
  6. ** Copyright (c) 1991, By Doctor Dissector            Last Update: 03/04/91 **
  7. \****************************************************************************/
  8.  
  9. /*
  10. ** XFDES - Version 1.05
  11. ** Copyright 1991 By Doctor Dissector
  12. **
  13. ** This program is free software; you can redistribute it and/or modify
  14. ** it under the terms of the GNU General Public License as published by
  15. ** the Free Software Foundation; either version 1, or (at your option)
  16. ** any later version.
  17. **
  18. ** This program is distributed in the hope that it will be useful,
  19. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ** GNU General Public License for more details.
  22. **
  23. ** You should have received a copy of the GNU General Public License
  24. ** along with this program; if not, write to the Free Software
  25. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27.  
  28. char xfdes_c_msg[]="xfdes v1.05 - 4/3/91 - (c)1991 - filename: xfdes.c";
  29.  
  30. /*=[ Include Files ]========================================================*/
  31.  
  32. #include "xfdes.h"
  33.  
  34. /*=[ Global Variables ]=====================================================*/
  35.  
  36. /* The C and D arrays used to calculate the key schedule. */
  37. static obpb1 C[28], D[28];
  38.  
  39. /* Key schedule. Alternating longs with low and high bits of key.
  40. ** Low and high 24 bits of keys are stored alternately.
  41. */
  42. sbpb24 KS[32];
  43.  
  44. /* The current block, divided into 2 halves. */
  45. obpb1 L[32], R[32];
  46.  
  47. /* Tables that combine the S and P operations. */
  48. sbpb24 S0L[64], S1L[64], S2L[64], S3L[64],
  49.        S4L[64], S5L[64], S6L[64], S7L[64],
  50.        S0H[64], S1H[64], S2H[64], S3H[64],
  51.        S4H[64], S5H[64], S6H[64], S7H[64];
  52.  
  53. /* It is slightly faster to indirect through this table than to specify
  54. ** the desired table directly.
  55. */
  56. static sbpb24 *stt[] =
  57.  {
  58.   S0L, S0H,
  59.   S1L, S1H,
  60.   S2L, S2H,
  61.   S3L, S3H,
  62.   S4L, S4H,
  63.   S5L, S5H,
  64.   S6L, S6H,
  65.   S7L, S7H,
  66.  };
  67.  
  68. /*===========================================================================*/
  69.  
  70. /* Convert unsl in twenty-four bit contiguous format
  71. ** to six bits per byte format.  Return result.
  72. */
  73. static sbpb24
  74. tfTOsixbit(tf)
  75.     ebpb24 tf;
  76. {
  77.     sbpb24 res=0;
  78.  
  79.  
  80.     res |= (tf >> 0) & 077;
  81.     res |= (((tf >> 6) & 077) << 8);
  82.     res |= (((tf >> 12) & 077) << 16);
  83.     return(res | (((tf >> 18) & 077) << 24));
  84. }
  85.  
  86. /*===========================================================================*/
  87.  
  88. /* Convert unsl in six bits per byte format
  89. ** to twenty-four bit contiguous format.  Return result.
  90. */
  91. static ebpb24
  92. sixbitTOtf(sb)
  93.     sbpb24 sb;
  94. {
  95.     ebpb24 res=0;
  96.  
  97.  
  98.     res |= ((sb >> 0) & 077);
  99.     res |= (((sb >> 8) & 077) << 6);
  100.     res |= (((sb >> 16) & 077) << 12);
  101.     return(res | (((sb >> 24) & 077) << 18));
  102. }
  103.  
  104. /*===========================================================================*/
  105.  
  106. /* Set up the key schedule from the key. */
  107. static void
  108. fsetkey(key)
  109.     obpb1 *key;
  110. {
  111.     reg int i, j, k;
  112.     obpb1 s, t;
  113.  
  114.  
  115.     /* First, generate C and D by permuting the key.  The low order bit of each
  116.     ** 8-bit char is not used, so C and D are only 28 bits apiece.
  117.     */
  118.     for(i=0; i<28; i++) {
  119.         C[i]=key[PC1_C[i]];
  120.         D[i]=key[PC1_D[i]];
  121.      }
  122.     /* To generate Ki, rotate C and D according to schedule and pick up a
  123.     ** permutation using PC2.
  124.     */
  125.     for(i=0; i<32; i+=2) {
  126.         for(k=0 ; k<shifts[i/2]; k++) {     /* rotate */
  127.             s=C[0];
  128.             t=D[0];
  129.             for(j=0; j<27; j++) {
  130.                 C[j]=C[j+1];
  131.                 D[j]=D[j+1];
  132.              }
  133.             C[27]=s;
  134.             D[27]=t;
  135.          }
  136.         /* Get Ki. Note C and D are concatenated. */
  137.         KS[i]=KS[i+1]=0;
  138.         for(j=0; j<24; j++) {
  139.             KS[i] |= ((sbpb24) C[PC2_C[j]] << j);
  140.             KS[i+1] |= ((sbpb24) D[PC2_D[j]] << j);
  141.          }
  142.         KS[i]=tfTOsixbit(KS[i]);
  143.         KS[i+1]=tfTOsixbit(KS[i+1]);
  144.      }
  145. }
  146.  
  147. /*===========================================================================*/
  148.  
  149. /* Lookup an S-box entry.*/
  150. static fbpb4
  151. lookupS(tableno, t6bits)
  152.     unsl tableno;
  153.     sbpb6R t6bits;
  154. {
  155.     sbpb6  fixed6bits;
  156.     fbpb4R r;
  157.     fbpb4  fixedr;
  158.  
  159.  
  160.     fixed6bits=(((t6bits >> 0) & 01) << 5)+
  161.                (((t6bits >> 1) & 01) << 3)+
  162.                (((t6bits >> 2) & 01) << 2)+
  163.                (((t6bits >> 3) & 01) << 1)+
  164.                (((t6bits >> 4) & 01) << 0)+
  165.                (((t6bits >> 5) & 01) << 4);
  166.     r=(fbpb4)S[(int)tableno][(int)fixed6bits];
  167.     fixedr=(((r >> 3) & 01) << 0)+
  168.            (((r >> 2) & 01) << 1)+
  169.            (((r >> 1) & 01) << 2)+
  170.            (((r >> 0) & 01) << 3);
  171.     return(fixedr);
  172. }
  173.  
  174. /*===========================================================================*/
  175.  
  176. /* The payoff: encrypt a block. */
  177. static void
  178. xform(quarters, saltvalue)
  179.     sbpb24 *quarters;
  180.     sbpb24 saltvalue;
  181. {
  182.     reg    sbpb6  *dp;
  183.     reg    int    mi;
  184.     reg    sbpb24 *kp, *kend, k;
  185.     static sbpb24 Dl, Dh;
  186.            sbpb24 Rl, Rh, Ll, Lh, negsalt;
  187.  
  188.  
  189.     negsalt = ~saltvalue;
  190.     Ll=Lh=Rl=Rh=0;
  191.     kend=&KS[32];
  192.     for(mi=24; mi>-1; mi--) {
  193.         for(kp=KS; kp<kend; ) {
  194.             reg sbpb24 **stp;
  195.             k=(Rl ^ Rh);
  196.             k &= (~negsalt);
  197.             Dl=(k ^( Rl ^ *kp++));
  198.             Dh=(k ^( Rh ^ *kp++));
  199.             stp=((sbpb24 **) stt);
  200.             dp=((sbpb6 *) & Dl);
  201.             Ll ^= (*stp++)[*dp];
  202.             Lh ^= (*stp++)[*dp++];
  203.             Ll ^= (*stp++)[*dp];
  204.             Lh ^= (*stp++)[*dp++];
  205.             Ll ^= (*stp++)[*dp];
  206.             Lh ^= (*stp++)[*dp++];
  207.             Ll ^= (*stp++)[*dp];
  208.             Lh ^= (*stp++)[*dp++];
  209.             dp=((sbpb6 *) & Dh);
  210.             Ll ^= (*stp++)[*dp];
  211.             Lh ^= (*stp++)[*dp++];
  212.             Ll ^= (*stp++)[*dp];
  213.             Lh ^= (*stp++)[*dp++];
  214.             Ll ^= (*stp++)[*dp];
  215.             Lh ^= (*stp++)[*dp++];
  216.             Ll ^= (*stp++)[*dp];
  217.             Lh ^= (*stp++)[*dp++];
  218.             k=(Ll ^ Lh);
  219.             k &= (~negsalt);
  220.             Dl=(k ^ Ll ^ *kp++);
  221.             Dh=(k ^ Lh ^ *kp++);
  222.             stp=((sbpb24 **) stt);
  223.             dp=((sbpb6 *) & Dl);
  224.             Rl ^= (*stp++)[*dp];
  225.             Rh ^= (*stp++)[*dp++];
  226.             Rl ^= (*stp++)[*dp];
  227.             Rh ^= (*stp++)[*dp++];
  228.             Rl ^= (*stp++)[*dp];
  229.             Rh ^= (*stp++)[*dp++];
  230.             Rl ^= (*stp++)[*dp];
  231.             Rh ^= (*stp++)[*dp++];
  232.             dp=((sbpb6 *) & Dh);
  233.             Rl ^= (*stp++)[*dp];
  234.             Rh ^= (*stp++)[*dp++];
  235.             Rl ^= (*stp++)[*dp];
  236.             Rh ^= (*stp++)[*dp++];
  237.             Rl ^= (*stp++)[*dp];
  238.             Rh ^= (*stp++)[*dp++];
  239.             Rl ^= (*stp++)[*dp];
  240.             Rh ^= (*stp++)[*dp++];
  241.          }
  242.         Ll ^= Rl;
  243.         Lh ^= Rh;
  244.         Rl ^= Ll;
  245.         Rh ^= Lh;
  246.         Ll ^= Rl;
  247.         Lh ^= Rh;
  248.      }
  249.     {
  250.         reg sbpb24 *qp;
  251.  
  252.  
  253.         qp=quarters;
  254.         *qp++=Ll;
  255.         *qp++=Lh;
  256.         *qp++=Rl;
  257.         *qp++=Rh;
  258.      }
  259. }
  260.  
  261. /*===========================================================================*/
  262.  
  263. /* Final permutation.  Takes input from globals L and R. */
  264. static void
  265. Fperm(block)
  266.     unsb *block;
  267. {
  268.     reg int j, k;
  269.  
  270.  
  271.     for(j=0; j<64; j++) {
  272.         k=FP[j];
  273.         block[j]=((k<32) ? L[k] : R[k-32]);
  274.      }
  275. }
  276.  
  277. /*===========================================================================*/
  278.  
  279. /* Inverse of E permutation. */
  280. static void
  281. undoe(fromarr, toarr)
  282.     obpb1 *fromarr, *toarr;
  283. {
  284.     reg int j;
  285.  
  286.  
  287.     for(j=0; j<32; j++)
  288.         toarr[j]=fromarr[1+(j & 03)+6*(j >> 2)];
  289. }
  290.  
  291. /*===========================================================================*/
  292.  
  293. static void
  294. toBA64(quarters, onebits64)
  295.     reg sbpb24 *quarters;
  296.     obpb1 *onebits64;
  297. {
  298.            int    j;
  299.     static obpb1  tmpE[48];
  300.     reg    unsb   *onebits48;
  301.     reg    sbpb24 quarter;
  302.  
  303.  
  304.     onebits48=tmpE;
  305.     quarter=sixbitTOtf(*quarters++);
  306.     for(j=0; j<24; j++)
  307.         *onebits48++=((quarter >> j) & 01);
  308.     quarter=sixbitTOtf(*quarters++);
  309.     for(j=0; j<24; j++)
  310.         *onebits48++=((quarter >> j) & 01);
  311.     undoe(tmpE,L);
  312.     onebits48=tmpE;
  313.     quarter=sixbitTOtf(*quarters++);
  314.     for(j=0; j<24; j++)
  315.         *onebits48++=((quarter >> j) & 01);
  316.     quarter=sixbitTOtf(*quarters++);
  317.     for(j=0; j<24; j++)
  318.         *onebits48++=((quarter >> j) & 01);
  319.     undoe(tmpE,R);
  320.     Fperm(onebits64);
  321. }
  322.  
  323. /*===========================================================================*/
  324.  
  325. static char *
  326. fcrypt(pw, salt)
  327.     const char *pw;
  328.     char *salt;
  329. {
  330.     reg    int    i;
  331.     reg    obpb1  j, c;
  332.     static obpb1  block[66];
  333.     static char   iobuf[16];
  334.     static sbpb24 out96[4];
  335.            sbpb24 saltvalue=0;
  336.  
  337.  
  338.     for(i=0; i<66; i++)
  339.         block[i]=0;
  340.     for(i=0; ((c=*pw)!=NULL) && (i<64); pw++) {
  341.         for(j=0; j<7; j++, i++)
  342.             block[i]=((c >> (6-j)) & 01);
  343.         i++;
  344.      }
  345.     fsetkey(block);
  346.     for(i=0; i<2; i++) {
  347.         c=iobuf[i]=*salt++;
  348.         if (c>'Z')
  349.             c-=6;
  350.         if (c>'9')
  351.             c-=7;
  352.         c-='.';
  353.         for(j=0; j<6; j++)
  354.             saltvalue |= ((c >> j) & 01) << (6*i+j);
  355.      }
  356.     saltvalue=tfTOsixbit(saltvalue);
  357.     xform(out96, saltvalue);
  358.     toBA64(out96, block);
  359.     for(i=0; i<11; i++) {
  360.         c=0;
  361.         for(j=0; j<6; j++) {
  362.             c <<= 1;
  363.             c |= block[6*i+j];
  364.          }
  365.         c+='.';
  366.         if (c>'9')
  367.             c+=7;
  368.         if (c>'Z')
  369.             c+=6;
  370.         iobuf[i+2]=c;
  371.      }
  372.     iobuf[i+2]=0;
  373.     if (!iobuf[1])
  374.         iobuf[1]=iobuf[0];
  375.     return(iobuf);
  376. }
  377.  
  378. /*===========================================================================*/
  379.  
  380. static void
  381. init(tableno, lowptr, highptr)
  382.     unsl tableno;
  383.     sbpb24 *lowptr, *highptr;
  384. {
  385.        reg    int k, i;
  386.     static  obpb1 tmp32[32];
  387.     static  obpb1 tmpP32[32];
  388.     static  obpb1 tmpE[48];
  389.            sbpb6R j;
  390.  
  391.  
  392.     for(j=0; j<64; j++) {
  393.         k=lookupS(tableno, j);
  394.         for(i=0; i<32; i++)
  395.             tmp32[i]=0;
  396.         for(i=0; i<4; i++)
  397.             tmp32[4*(int)tableno+i]=(obpb1)(k >> i) & 01;
  398.         for(i=0; i<32; i++)
  399.             tmpP32[i]=tmp32[P[i]-1];
  400.         for(i=0; i<48; i++)
  401.             tmpE[i]=tmpP32[(int)E[i]-1];
  402.         lowptr[j]=highptr[j]=0;
  403.         for(i=0; i<24; i++)
  404.             lowptr[(int)j] |= (unsl)tmpE[i] << i;
  405.         for(k=0, i=24; i<48; i++, k++)
  406.             highptr[(int)j] |= (unsl)tmpE[i] << k;
  407.         lowptr[j]=tfTOsixbit(lowptr[j]);
  408.         highptr[j]=tfTOsixbit(highptr[j]);
  409.      }
  410. }
  411.  
  412. /*===========================================================================*/
  413.  
  414. static void
  415. init_des(void)
  416. {
  417.     init((unsl)0, S0L, S0H);
  418.     init((unsl)1, S1L, S1H);
  419.     init((unsl)2, S2L, S2H);
  420.     init((unsl)3, S3L, S3H);
  421.     init((unsl)4, S4L, S4H);
  422.     init((unsl)5, S5L, S5H);
  423.     init((unsl)6, S6L, S6H);
  424.     init((unsl)7, S7L, S7H);
  425. }
  426.